package com.dkts.oil.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkts.oil.config.OilConfig;
import com.dkts.oil.converter.*;
import com.dkts.oil.mysql.entity.OilObstacle;
import com.dkts.oil.strtree.GeoJsonSTRtreeIndex;
import com.dkts.oil.util.GeoAreaUtil;
import com.dkts.oil.web.vo.request.OilCustomTankRequest;
import com.dkts.oil.web.vo.request.OilStorageTankRequest;
import com.dkts.oil.web.vo.response.subReq.OilStorageAddress;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Geometry;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  储备库选址实现类
 * </p>
 *
 * @author heguanglong
 * @since 2025-04-10
 */
@Service
@Slf4j
public class OilAddressServiceImpl   {



    @Autowired
    private OilConfig config;

    @Autowired
    private OilAddressConverter addressConverter;
    /**
     * 获取储备库选址
     * @param request
     * @return
     */
    public List<OilStorageAddress> oilStorage(OilStorageTankRequest request) throws Exception {
        //选址区域
        Geometry geometry=GeometryConverter.convert(request.getAddressArea().getBoundWkt());
        WriterToFile.writerFileGeoJson(config.getTempDir()+"/选址.geojson",geometry);

        //与选址区域有个缓冲区域是不能规划的
        Geometry bufferGeometry=GeometryConverter.getBuff(geometry,request.getAddrBuffer());
        WriterToFile.writerFileGeoJson(config.getTempDir()+"/缓冲区域.geojson",bufferGeometry);

        //规划区域
        Geometry planGeometry=GeometryConverter.getBuff(bufferGeometry,request.getPlanAreaWidth());
        WriterToFile.writerFileGeoJson(config.getTempDir()+"/规划区域.geojson",planGeometry);

        //真正的规划区域
        Geometry planGeometry2= GeometryConverter.difference(planGeometry,bufferGeometry);
        WriterToFile.writerFileGeoJson(config.getTempDir()+"/规划区域取反.geojson",planGeometry2);
        return custom(request,planGeometry2,"oilStorage");
    }

    public List<OilStorageAddress> custom(OilStorageTankRequest request, Geometry planGeometry2,String type) throws Exception {
        //查询障碍物
        List<Geometry> listObstacles= addressConverter.listObstacles(planGeometry2,request,config.getTempDir()+"/"+type);
        double gridLength = GeoAreaUtil.calculateSizeByArea(request.getSurfaceArea());
        List<Geometry> list= UtilsGeoGrid.gridGeometryInfo(planGeometry2, BoundingBoxUtil.getBoundingBox(planGeometry2),true, gridLength,listObstacles,config.getTempDir(),3000,4);
        if(list == null || list.size() ==0){
            return new ArrayList<>();
        }
        WriterToFile.writerFileGeoJson(config.getTempDir()+"/"+type+".geojson",GeometryConverter.getGeoJsonResult(list));
        return GeometryConverter.oilStorage(gridLength,list,request.getNumber(),type,planGeometry2);
    }


    public List<OilStorageAddress> custom(OilCustomTankRequest request) throws Exception {
        OilStorageTankRequest request1 = new OilStorageTankRequest();
        BeanUtils.copyProperties(request,request1);
        Geometry geometry=GeometryConverter.convert(request.getAddressArea().getBoundWkt());
        WriterToFile.writerFileGeoJson(config.getTempDir()+"/选址-custom.geojson",geometry);
        return custom(request1,geometry,"custom");
    }
}
